package com.gyf.backoffice.web.controller;

import com.gyf.backoffice.model.Items;
import com.gyf.backoffice.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping("list")
    public ModelAndView list(){

        ModelAndView mv = new ModelAndView("items/list1");
        List<Items> allItems = itemsService.findAllItems();
        mv.addObject("itemsList", allItems);
        return    mv;

    }


    @RequestMapping("save")
    public ModelAndView save(){

        Items items = new Items();
        items.setName("鸽子蛋");
        items.setPrice(100.5F);
        items.setCreatetime(new Date());
        items.setDetail("sdfasdf");
        itemsService.saveOrupdate(items);
        return list();
    }

    @RequestMapping("delete")
    public ModelAndView delete(Integer id){

        itemsService.delete(id);
        return list();
    }

    @RequestMapping("edit")
    public ModelAndView update(Integer id){

        Items itemsOne = itemsService.findItemsOne(id);
        ModelAndView mv = new ModelAndView("items/edit");
        mv.addObject("items", itemsOne);
        return mv;
    }

    @RequestMapping("update")
    public ModelAndView update(Items items){

        itemsService.update(items);
        return list();
    }

}
